package com.mysoft.gfyx.marketdevelop.service.impl;

import com.mysoft.framework.common.exception.BusinessLogicException;
import com.mysoft.framework.service.AppService;
import com.mysoft.gfyx.marketdevelop.controller.dto.GetProjectContactDetailRequestDto;
import com.mysoft.gfyx.marketdevelop.mip.GetDeveloperDataService;
import com.mysoft.gfyx.marketdevelop.mip.GetProjectDataService;
import com.mysoft.gfyx.marketdevelop.mip.dto.GetDeveloperContactsRequestDto;
import com.mysoft.gfyx.marketdevelop.mip.dto.TjCustom.GetProjectDetailTableRequestDto;
import com.mysoft.gfyx.marketdevelop.mip.dto.TjCustom.MohurdProjectListRequestDto;
import com.mysoft.gfyx.marketdevelop.mip.dto.TjCustom.SqlApiResponseDto;
import com.mysoft.gfyx.marketdevelop.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;


@Service
public class ProjectServiceImp extends AppService implements ProjectService {

    @Autowired
    private GetDeveloperDataService getDeveloperDataService;
    @Autowired
    private MohurdProjectService mohurdProjectService;
    @Autowired
    private GetProjectDataService getProjectDataService;
    @Autowired
    private ProvinceRadarService provinceRadarService;

    @Autowired
    private CommonService commonService;

    public Map<String, Object> getProjectMainData(String projectId) {
        MohurdProjectListRequestDto dto = new MohurdProjectListRequestDto();
        dto.setPageNum(1);
        dto.setPageSize(10);
        dto.setId(projectId);
        SqlApiResponseDto res = getDeveloperDataService.mohurdProjectList(dto);
        if (res.getData() == null || res.getData().getRows().size() == 0) {
            throw new BusinessLogicException("未找到相关项目数据:" + projectId);
        }
        Map<String, Object> result = new HashMap<>();
        AtomicReference<Map<String, Object>> finalResult = new AtomicReference<>(result);
        res.getData().getRows().forEach(p -> {
            mohurdProjectService.processMohurdProject(p, 1);
            //联系人数量
            GetDeveloperContactsRequestDto getDeveloperContactsRequestDto = new GetDeveloperContactsRequestDto();
            getDeveloperContactsRequestDto.setDeveloper_organizing_code(p.get("organizing_code_string").toString());
            getDeveloperContactsRequestDto.setPageSize(100000);
            Map<String, Integer> contactCount = provinceRadarService.getContactCount(getDeveloperContactsRequestDto);
            if (contactCount.containsKey(p.get("organizing_code_string").toString())) {
                p.put("contact_num", contactCount.get(p.get("organizing_code_string").toString()));
            }
            //数据状态
            if (p.get("id") != null) {
                p.put("oppty_status", commonService.getOpptyStatus(CommonService.BigDataType.BUILDING.toString(), p.get("id").toString(), true));
            }
            finalResult.set(p);
        });
        return finalResult.get();
    }

    public Map<String, Object> getProjectContactDetail(GetProjectContactDetailRequestDto dto) {
        GetProjectDetailTableRequestDto getProjectDetailTableRequestDto = new GetProjectDetailTableRequestDto();
        getProjectDetailTableRequestDto.setId(dto.getId());
        getProjectDetailTableRequestDto.setPageNum(1);
        getProjectDetailTableRequestDto.setPageSize(10);
        SqlApiResponseDto res = new SqlApiResponseDto();
        if (dto.getType().equals("supplier_contact")) {
            res = getProjectDataService.getProjectSupplierContact(getProjectDetailTableRequestDto);
        } else {
            res = getProjectDataService.getProjectDeveloperContact(getProjectDetailTableRequestDto);
        }

        if (res.getData() == null || res.getData().getRows().size() == 0) {
            throw new BusinessLogicException("未找到在建项目联系人数据");
        }
        Map<String, Object> result = new HashMap<>();
        AtomicReference<Map<String, Object>> finalResult = new AtomicReference<>(result);
        res.getData().getRows().forEach(p -> {
            finalResult.set(p);
        });
        return finalResult.get();
    }
}
